<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class StoreController extends Controller
{
    /**
     * 显示添加页面
     */

    public function getAdd()
    {
    	return view('admin.store.add');
    }

    /**
     * 执行添加
     */
    public function postInsert(Request $request)
    {
    	//进行表单验证
    	$this->validate($request,[
    		'bname' => 'required',
    		'bprice' => 'required',	
    		'bauthor' => 'required',	
    		'bprofile' => 'required'	

    		],[
    		'bname.required' => '书籍名称不能为空',
    		'bprice' => '价格不能为空',
    		'bauthor' => '作者不能为空',
    		'bprofile' => '书籍封面不能为空'
    		]);
    	//获取数据
    	$data = $request->except(['_token']);

    	//处理书籍封面
    	if($request->hasFile('bprofile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('bprofile')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('bprofile')->move('./Uploads/',$fileName);
    		$data['bprofile'] = '/Uploads/'.$fileName;
    	}
    	//插入数据库
    	$res = DB::table('store')->insert($data);
    	
    	//检测
    	if($res){
    		return redirect('/store/index')->with('info','添加成功');
    	}else{
    		return back()->with('error','添加失败');
    	}
    }

    /**
     * 书籍列表显示
     */
    public function getIndex(Request $request){
    	//读取数据库
    	$data = DB::table('store')->where(function($query) use($request){
    		$k = $request->input('keyword');
    		if(!empty($k)){
    			$query->where('bname','like','%'.$k.'%');
    		}
    	})->paginate($request->input('num',5));
    	// dd($data);
    	return view('admin.store.index',['data'=>$data,'request'=>$request]);
    }

    /**
     * 用户的修改页面
     */
    public function getEdit(Request $request)
    {
    	//读取当前id的用户信息
    	$id = $request->input('id'); 
    	//读取数据库
    	$data = DB::table('store')->where('id',$id)->first();
    	// dd($data);
    	$path = $request->path();
    	$path .= '?id='.$id;
    	session(['redirect'=>$path]);

    	//解析模板
    	return view('admin.store.edit',['data'=>$data,'request'=>$request]);
    }

    /**
     * 用户的更新操作
     */
    public function postUpdate(Request $request)
    {
    	//表单验证
    	$this->validate($request,[
    		'bname' => 'required',
    		'bprice' => 'required',	
    		'bauthor' => 'required'
    		],[
    		'bname.required' => '书籍名称不能为空',
    		'bprice' => '价格不能为空',
    		'bauthor' => '作者不能为空'
    		]);

    	//图片上传操作
    	$data = $request->except(['_token','id']);

    	//处理上传文件
    	$path = $this->upload($request);
    	if(!empty($path)){
    		$data['bprofile'] = $path;
    	}

    	//更新
    	$res = DB::table('store')->where('id',$request->input('id'))->update($data);

    	if($res){
    		if(session('redirect')){
    			return redirect(session('redirect'))->with('info','修改成功');
    		}
    		return redirect('/store/index')->with('info','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}
    	
    }

    /**
     * 删除操作
     */
    public function getDetele()
    {
    	$id = $request->input('id');
    	//执行删除
    	$res  = DB::table('store')->where('id',$id)-> delete();
    	if($res){
    		return back()->with('info','删除成功');
    	}else{
    		return back()->with('error','删除成功');
    	}
    }

    /**
     * 封装上传操作代码
     */
    public function upload($request)
    {
    	if($request->hasFile('profile')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads/',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    		return $data['profile'];
    	}
    }

    /**
     * 前台书籍列表页
     */
    public function  show()
    {
    	//读取数据库
    	$data = DB::table('store')->get();
    	// dd($data);
    	//显示列表页面
    	return view('home.store.index',['data'=>$data]);
    }



}
